package th.co.bjc.md.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import co.th.bjc.md.process.service.CallService;
import co.th.bjc.md.service.interfaces.LoginService;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger _logger = Logger.getLogger("web");
       
    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String forwardPage = "";
		HttpSession session = request.getSession(true);
		String transaction_id = (String) session.getAttribute("Transaction_id");
		try{
			String action = request.getParameter("action");
			if(action == null){

				forwardPage = "/jsp/login.jsp";
				getServletContext().getRequestDispatcher(forwardPage).forward(request, response);
			}else if (action.equalsIgnoreCase("Login")){
				this.login(transaction_id, request, response);
			}else if (action.equalsIgnoreCase("Logout")){
				this.logout(request, response);
			}else if (action.equalsIgnoreCase("Forget")){
				this.forget(request, response);
			}else if (action.equalsIgnoreCase("Register")){
				this.register(request, response);
			}
			
		}catch(Exception ex){
			
		}
	}
	
	
	
	private void login(String transaction_id, HttpServletRequest request, HttpServletResponse response){
		String forwardPage = "";
		try{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			_logger.info(transaction_id + "- Username : " + username + " <> Password : " + password);
			LoginService login = (LoginService) CallService.getService("login");
			if(login.validate(username, password)){

				HttpSession session = request.getSession();
				
			    session.setAttribute("userID", username);
//				forwardPage = "RegisterVisitorServlet";
			    forwardPage = "DataBusinessServlet";
//				getServletContext().getRequestDispatcher(forwardPage).forward(request, response);
			}else{
				forwardPage = "LoginServlet";
			}
			response.sendRedirect(forwardPage);
		}catch(Exception ex){
			_logger.error(LoginServlet.class.getName()+" : "+ ex);
		}
	}

	private void logout(HttpServletRequest request, HttpServletResponse response){
		try{
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("LoginServlet");
		}catch(Exception ex){
			
		}
	}
	
	private void forget(HttpServletRequest request, HttpServletResponse response){
		try{
			
		}catch(Exception ex){
			
		}
	}
	
	private void register(HttpServletRequest request, HttpServletResponse response){
		try{
			
		}catch(Exception ex){
			
		}
	}
	
//	private void initNewLetters(HttpSession session) {
//		// TODO Auto-generated method stub
//		try{
//			NewLettersImpl impl = new NewLettersImpl();
//			session.setAttribute("listNewLettersTop", impl.getNewLettersTop());
//		}catch(Exception ex){
//			_logger.error(LoginServlet.class.getName()+" : "+ ex);
//		}
//	}
	

}
